package hsp.admin.aop;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class LoggerAspect {

//    private final ISystemLogService systemLogService;
//
//    private LoggerAspect(ISystemLogService systemLogService){
//        this.systemLogService = systemLogService;
//    }

    /**
     * execution() 表达式的主体
     * com.boot.api.ApiResult 返回值类型， 可用*表示任意类型
     * com.boot aop所切的服务包名
     * .. 当前包及子包
     * * 第一个*表示所有类
     * .*(..) .*表示任何方法名，括号表示参数，两个点表示任意参数类型
     */
    @Pointcut("execution(hsp.admin.api.ApiResult hsp.admin.app..*.*(..))")
    public void controller(){}

    /**
     * 查看请求体
     * @param joinPoint
     */
    @Before("controller()")
    public void beforeController(JoinPoint joinPoint){
        log.info("==========Controller Request Before Start==========");
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = methodSignature.getMethod().getName();
        log.info("Request Method:{}", className + "." + methodName + "()");
        Arrays.stream(methodSignature.getParameterNames()).forEach(ele -> log.info("Request Parameter: {}", ele));
        Arrays.stream(joinPoint.getArgs()).forEach(ele -> log.info("Request Args: {}", ele));
        log.info("==========Controller Request Before End==========");
    }
//
//
//    @Pointcut("@annotation(hsp.admin.aop.SystemLogger)")
//    public void SystemLogger(){}
//
//    /**
//     * 记录日志表
//     * @param proceedingJoinPoint
//     * @return
//     * @throws Throwable
//     */
//    @Around("SystemLogger()")
//    public Object aroundController(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        log.info("==========Controller Request Around Start==========");
//        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
//        SystemLogger annotation = signature.getMethod().getAnnotation(SystemLogger.class);
//        Object[] args = proceedingJoinPoint.getArgs();
//
//        SystemLogPO systemLog = new SystemLogPO();
//        List<LoggerType> logTypes = Arrays.asList(annotation.type());
//        boolean edit = new HashSet<>(logTypes).containsAll(getEditLogTypes());
//        Long bizId;
//        if(edit){
//            bizId = JSONObject.from(proceedingJoinPoint.getArgs()[0]).getLong("id");
//            if(bizId == null){
//                systemLog.setType(LoggerType.INSERT.toString());
//            }else{
//                systemLog.setType(LoggerType.UPDATE.toString());
//            }
//            systemLog.setObjectJson(JSONArray.toJSONString(args));//参数要在执行前赋值
//        }
//
//        Object proceed = proceedingJoinPoint.proceed(args);//流程处理
//
//        if(edit){
//            bizId = JSONObject.from(proceedingJoinPoint.getArgs()[0]).getLong("id");
//            systemLog.setBizId(bizId);
//        }else if(logTypes.contains(LoggerType.DELETE)){
//            systemLog.setType(LoggerType.DELETE.toString());
//            systemLog.setBizId((Long) args[0]);
//        }
//        String className = proceedingJoinPoint.getTarget().getClass().getAnnotation(SystemLogger.class).objectClass().getName();
//
////        TableName tableName = (TableName)proceedingJoinPoint.getTarget().getClass().getAnnotation(SystemLogger.class).objectClass().getAnnotation(TableName.class);
//        systemLog.setTableName(className);
//
//        systemLogService.save(systemLog);
//        log.info("==========Controller Request Around End==========");
//        return proceed;
//    }
//
//    private ArrayList<LoggerType> getEditLogTypes(){
//        return new ArrayList<>(){{
//            add(LoggerType.INSERT);
//            add(LoggerType.UPDATE);
//        }};
//    }

}
